package com.gaber.mapreduce.wordcount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @author gaber
 * @date 2023/12/16 15:52:17
 * @description TODO
 * KEYIN, map阶段输入的key类型：LongWritable
 * VALUEIN, map阶段输入的value类型：Text
 * KEYOUT,  map阶段输入的key类型：Text
 * VALUEOUT map阶段输入的value类型：IntWritable
 */
public class WorkCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

    private Text outK = new Text();

    private IntWritable outV = new IntWritable();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        // 1 获取一行
        String line = value.toString();

        // 2 切割
        String[] words = line.split(" ");

        // 3 循环写出
        for (String word : words) {
            outK.set(word);
            context.write(outK, outV);
        }
    }
}
